package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import com.sky.entity.BusinessVO;
import com.sky.entity.Top10Sales;
import com.sky.vo.OrderVO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;


/**
 * @author 苏成博
 * @date 2023-02-13 10:46
 */
@Mapper
public interface OrderMapper {
    @Insert("insert into orders(number,status,user_id,address_book_id," +
            "order_time,checkout_time,pay_method,pay_status," +
            "amount,remark,user_name,phone," +
            "address,consignee,cancel_reason,rejection_reason," +
            "cancel_time,estimated_delivery_time,delivery_status,delivery_time," +
            "pack_amount,tableware_number,tableware_status) " +
            "values" +
            "(#{number},#{status},#{userId},#{addressBookId},#{orderTime},#{checkoutTime},#{payMethod},#{payStatus},#{amount}" +
            ",#{remark},#{userName},#{phone},#{address},#{consignee},#{cancelReason},#{rejectionReason},#{cancelTime}," +
            "#{estimatedDeliveryTime},#{deliveryStatus},#{deliveryTime},#{packAmount},#{tablewareNumber},#{tablewareStatus})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void addOrder(Orders orders);

    Page<OrderVO> findHistoryOrders(OrdersPageQueryDTO ordersPageQueryDTO);

    OrderVO findOrdersById(Long id);

    void updateOrder(Orders orders);

    @Select("select COUNT(*) as count ,status from orders where user_id=1 group by status")
    List<BusinessVO> statisticsOrders();//todo

    @Select("select * from orders where status=#{status} and order_time < #{plusMinutes}")
    List<Orders> timeOutOrders(Integer status, LocalDateTime plusMinutes);

    @Select("select sum(amount) from orders where order_time between #{begin} and #{end} and status = #{status}")
    Double getTurnoverData(Map<String, Object> map);

    @Select("select count(id) from orders where order_time between #{begin} and #{end} and status = #{status}")
    Integer findCompletedOrders(Map<String, Object> map);

    @Select("select count(id) from orders where order_time between #{begin} and #{end}")
    Integer findTotalOrders(Map<String, Object> map);

    @Select("select count(*) from orders")
    Double findAllTotalOrders();

    @Select("select count(*) from orders where status=#{status}")
    Double findAllValidOrders(Integer status);
    @Select("select od.name , sum(od.number) as number  " +
            "from order_detail  od ,orders  o " +
            "where o.id=od.order_id  " +
            "and o.order_time between #{begin} and #{end} " +
            "group by od.dish_id order by number ")
    List<Top10Sales> getSalesTop10ReportVO(LocalDate begin, LocalDate end);
}
